package concurrent.threadpool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 线程池创建
 *
 * @author LoneRanger
 */
public class ThreadPoolDemo {

	public static void main(String[] args) {

		//只有一个线程执行的线程池，任务会依次一一执行
		//ExecutorService threadPool = Executors.newSingleThreadExecutor();

		//一个定长线程池 核心线程数 和 最大线程数相等，超出的线程会在队列中等待
		//ExecutorService threadPool = Executors.newFixedThreadPool(5);

		//一个可缓存的线程池，核心线程数0，最大线程数Integer.MAX_VALUE
		ExecutorService threadPool = Executors.newCachedThreadPool();

		try {
			for (int i = 0; i < 100; i++) {
				final int tempInt = i;
				threadPool.execute(() -> {
					System.out.println("执行" + tempInt);
				});
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			threadPool.shutdown();
		}
	}

}
